<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <div class="side_scroll_y">
      <el-tree
        :data="treeInfo.data"
        highlight-current
        :props="treeInfo.defaultProps"
        node-key="id"
        :default-checked-keys="[treeInfo.checked]"
        @node-click="handleNodeClick"
        :default-expand-all="true"
        :expand-on-click-node="false"
      />
    </div>
  </page-dialog>
</template>

<script setup name="SelectEquipment">
// 组织机构查询
import PageDialog from "../PageDialog/index";
import { getDeptTrees } from "@/api/core.js";
const { proxy } = getCurrentInstance()
const emit = defineEmits(['showDialog', 'callback', 'reset'])
const props = defineProps({
  showDialog: {
    type: Boolean,
    default: false,
  },
})
let shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emit('update:showDialog', newValue) // 更新父组件shows
  },
})

onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    getList()
  })
})

// 弹窗相关
const dialogInfo = reactive({
  title: '人员选择',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: true,
      event: save,
      show: true,
    },
  ],
})
const treeInfo = reactive({
  data: [],
  nodeKey: 'code',
  checked: [],
  defaultProps: {
    children: 'children',
    label: 'name',
  },
  selectNode: {},
})

// 获取列表
function getList () {
  getDeptTrees().then((res) => {
    if (res.success) {
      treeInfo.data = res.data
    }
  })
}
function handleNodeClick (node) {
  dialogInfo.btList[1].disabled = !node
  treeInfo.selectNode = node
}
// 关闭弹窗前的回调
function handleClose () {
  shows.value = false
  emit('reset')
}
function save () {
  if (treeInfo.selectNode) {
    handleClose()
    emit('callback', treeInfo.selectNode)
  }
}
</script>

<style scoped></style>
